package org.jeecg.modules.rental.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.rental.entity.SiteProfits;
import org.jeecg.modules.rental.mapper.SiteProfitsMapper;
import org.jeecg.modules.rental.service.ISiteProfitsService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.Map;

/**
 * @Description: 场地方分润表
 * @Author: jeecg-boot
 * @Date:   2025-02-11
 * @Version: V1.0
 */
@Service
public class SiteProfitsServiceImpl extends ServiceImpl<SiteProfitsMapper, SiteProfits> implements ISiteProfitsService {
    @Override
    public List<Map<String, Object>> getSiteProfitsStatistics(String type) {
        // 根据统计纬度生成时间格式
        String dateFormat;
        switch (type.toLowerCase()) {
            case "year":
                dateFormat = "%Y"; // 按年统计
                break;
            case "quarter":
                dateFormat = "%Y-Q%q"; // 按季度统计
                break;
            case "month":
                dateFormat = "%Y-%m"; // 按月统计
                break;
            case "week":
                dateFormat = "%Y-%u"; // 按周统计
                break;
            case "day":
                dateFormat = "%Y-%m-%d"; // 按日统计
                break;
            default:
                throw new IllegalArgumentException("不支持的统计纬度：" + type);
        }

        // 使用 MyBatis Plus 的 QueryWrapper 查询数据库
        QueryWrapper<SiteProfits> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DATE_FORMAT(start_time, '" + dateFormat + "') AS `key`",
                        "SUM(income) AS `value`") // 统计分润金额
                .groupBy("DATE_FORMAT(start_time, '" + dateFormat + "')") // 按时间纬度分组
                .orderByAsc("`key`"); // 时间升序排列

        // 查询分润数据
        List<Map<String, Object>> result = this.baseMapper.selectMaps(queryWrapper);

        return result;
    }
}
